package com.yocard.yxyd.adapter;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.ValueCallback;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.yocard.yxyd.R;
import com.yocard.yxyd.base.AppContext;
import com.yocard.yxyd.http.model.response.CsmOrderListBean;
import com.yocard.yxyd.utils.CommonUtils;
import com.yocard.yxyd.utils.util.TimeUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created on 2019/4/3.
 */
public class CsmListAdapter extends RecyclerView.Adapter<CsmListAdapter.ViewHolder> {

    private final int TYPE_TOP = 0x11;
    private final int TYPE_NORMOL = 0x22;
    private final int TYPE_BOTTOM = 0x33;
    private List<CsmOrderListBean.OrderList> datas;

    public CsmListAdapter(List<CsmOrderListBean.OrderList> datas) {
        this.datas = datas;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View inflate = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_customer, viewGroup, false);
        return new ViewHolder(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {

        try {
            CsmOrderListBean.OrderList itemBean = datas.get(i);
            viewHolder.tvName.setText(itemBean.getSheets().getName());
            viewHolder.tvType.setText(CommonUtils.getLoanTypeStr(itemBean.getLoan_type()));
            viewHolder.tvTime.setText(TimeUtils.millis2String(itemBean.getAction_at() * 1000));
            viewHolder.tvAmount.setText(itemBean.getSheets().getAmount() + "万");
            viewHolder.tvPeriord.setText(CommonUtils.getMonth(itemBean.getSheets().getLoan_period()));

            viewHolder.itemView.setTag(itemBean);
            viewHolder.itemView.setOnClickListener(clickListener);

            if (itemBean.getTrack_noce() != null) {
                viewHolder.tvStatus.setText(itemBean.getTrack_noce().getHandleStatus());
                viewHolder.tvStatus.setTextColor(AppContext.getColor_(CommonUtils.getStatusColor(itemBean.getTrack_noce().getType())));
            } else {
                viewHolder.tvStatus.setText("");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return TYPE_TOP;
        } else if (position == getItemCount() - 1) {
            return TYPE_BOTTOM;
        } else {
            return TYPE_NORMOL;
        }
    }

    @Override
    public int getItemCount() {
        return datas == null ? 0 : datas.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        RelativeLayout rlLayout;
        @BindView(R.id.tv_name)
        TextView tvName;
        @BindView(R.id.tv_type)
        TextView tvType;
        @BindView(R.id.tv_time)
        TextView tvTime;
        @BindView(R.id.tv_amount)
        TextView tvAmount;
        @BindView(R.id.tv_status)
        TextView tvStatus;
        @BindView(R.id.tv_periord)
        TextView tvPeriord;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    View.OnClickListener clickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (callback != null) {
                CsmOrderListBean.OrderList itemBean = (CsmOrderListBean.OrderList) v.getTag();
                callback.onReceiveValue(itemBean);
            }
        }
    };

    private ValueCallback<CsmOrderListBean.OrderList> callback;

    public void setOnItemClickListener(ValueCallback<CsmOrderListBean.OrderList> callback) {
        this.callback = callback;
    }

}
